import Q from "../lib/qunee-es";
import EventEmitter from "events";

import {createMap,MapGraph} from "./tools/MapGraph"


import GridBackground from "./tools/GridBackground"
import ImageBackground from "./tools/ImageBackground"



class PowerNet extends EventEmitter {
	constructor(options) {
		super();
		this.uuid = "" + Math.random();
		this.options = options;
		this.elements = {};
		this.tempElements = {}; // tempId: element , 在请求服务前的节点集合
		this.graph = null
		// var graph = null;
		// if (options.graph) {
		// 	graph = options.graph;
		// } else if (options.target) {
		// 	// graph = new Q.Graph(this.options.target);
		// 	var m = createMap({target:options.target ,lat:31.25, lng:121.45, zoom:7 })
		// 	graph = new MapGraph(m)
		// }
		
	}

	setGraph(graph) {
		var options = this.options
		this.graph = graph
		if (options.editable) {
			graph.callLater(function () {
				graph.editable = true;
			});
		}
		this.graph = graph;
		Q.Defaults.LABEL_EDITOR_SUBMIT_WHEN_LOST_FOCUS = true;
		if (this.options.showGridBackground) {
			this.gridBackground = new GridBackground(this.graph);
			// this.initGridBackground()
		}
		if(this.options.imageBackground) {
			this.setImageBackground(this.options.imageBackground)
		}
	}

	setImageBackground(imageUrl , options) {
		if(!this.imageBackground) {
			this.imageBackground = new ImageBackground(this.graph ,options)
		}
		this.imageBackground.image = imageUrl
	}

	createElement(element, tempId, sender) {
		// var e = this.tempElements[tempId];
		var e;
		var props = element.Props;
		// if(e)
		// if (!e) {
		var x = props.x
		var y = props.y
		
		var graph= this.graph
		if (element.Type == 1 || element.Type == 5) {
			e = new Q.Node();
			if (element.Type == 5) {
				//subnetwork
				e.enableSubNetwork = true;
			}
		}
		if (element.Type == 2) {
			//edge
			e = new Q.Edge();
			Object.assign(e, {
			// 	name: props.name,
				from: this.elements[props.from],
				to: this.elements[props.to],
			});
		}
		if (element.Type == 3) {
			//text
			e = new Q.Text();
		}
		if (element.Type == 4) {
			//group
			e = new Q.Group();
			// Object.assign(e, {
			// 	name: props.name,
			// });
			// if ("x" in props) {
			// 	e.location = new Q.Point(e.x, e.y);
			// }
		}
		Object.assign(e, {
			name:props.name,
			element: element,
			id: element.Id,
			sender: element.Sender || -1,
		});
		e.set("id", element.Id);
		this.setLocation(e , x , y ,props.zoom)
		
		// }
		//设置属性
		var skipProps = { name: true, x: true, y: true, from: true, to: true, parent: true };
		this.setProps(e, props, skipProps);

		//设置parent
		if (props.parent) {
			parent = this.elements[props.parent];
			if (parent) {
				e.parent = parent;
			}
		}

		
		

		this.elements[element.Id] = e;
		// if(!this.tempElements[tempId]) {
		this.graph.graphModel.add(e);
		// }
	}

	getElement(id) {
		return this.elements[id];
	}

	setProps(ele, props, skipProps) {
		if (!ele) {
			return;
		}
		for (var k in props) {
			if (skipProps && skipProps[k]) {
				continue;
			}
			if (k == "styles") {
				var styles = props[k];
				for (var s in styles) {
					ele.setStyle(s, styles[s]);
				}
				continue;
			}
			ele[k] = props[k];
		}
	}

	// 如果是MapGraph x,y是经纬度
	setLocation(ele,x , y , zoom) {// x ,y要转换成经纬度
		if(ele instanceof Q.Edge) {
			return
		}
		if(this.isMapGraph) {
			// var ll = this.graph.getLonLatFromPixel(x,y)
			ele.lonLat = this.graph.toLatLng(x,y)
			var p = this.graph.getPixelFromLonLat(ele.lonLat)
			x = p.x
			y = p.y
			if(zoom) {
				ele.zoom = zoom
			}
		}
		ele.x = x
		ele.y = y
		if(ele instanceof Q.Group) {
			ele.location = new Q.Point(ele.x, ele.y);
		}
	}
	clear() {
		this.graph.clear();
		this.elements = {};
	}

	removeElement(id) {
		var e = this.elements[id];
		if (e) {
			delete this.elements[id];
			this.graph.removeElement(e);
		}
	}

	loadElements(elements) {
		for (var i = 0; i < elements.length; i++) {
			var element = elements[i];
			if (element.Type == 4) {
				//create group first
				this.createElement(element);
			}
		}
		for (var i = 0; i < elements.length; i++) {
			var element = elements[i];
			if (element.Type == 5) {
				//create subnetwork parent first
				this.createElement(element);
			}
		}
		for (var i = 0; i < elements.length; i++) {
			var element = elements[i];
			if (element.Type == 1 || element.Type == 3) {
				//create group node and text
				this.createElement(element);
			}
		}
		for (var i = 0; i < elements.length; i++) {
			var element = elements[i];
			if (element.Type == 2) {
				//create edge last
				this.createElement(element);
			}
		}
	}

	setMovable(b) {
		this.graph.forEach(function (e) {
			e.movable = b;
		});
	}

	hasNode(id) {
		return id && id in this.elements;
		// let me = this

		// let elements = me.data.Elements
		// for(var i in elements){
		// 	if(elements[i].Id == id){
		// 		return true
		// 	}
		// }
		// return false
	}
	currentSubNetworkId() {
		var parent = this.graph.currentSubNetwork;
		if (parent) {
			return parent.id;
		}
		return null;
	}
	isSender(ele) {
		//我是发出者吗？false：被动接受者
		var sender = ele.get("sender");
		return !sender || sender == this.uuid;
	}
	get isMapGraph() {
		return this.graph instanceof MapGraph
	}
}


// var graph = new Q.Graph('canvas');
// graph.originAtCenter = false;

// var background = new ImageBackground(graph, {
//     onload: function () {
//         loadDatas();
//         graph.limitedBounds = background.bounds;
//         graph.zoomToOverview();
//     }
// });

// background.image = 'mapchart/map_background.jpg';
export default PowerNet